Repository navigation
WebP: forward-only streaming decode of lossy stills - #21
Merged
Merged
Conversation
WebpImageDecoder implements IStreamingImageDecoder. A lossy still (simple VP8, or VP8X with VP8 and optional ALPH) decodes macroblock row by macroblock row as the VP8 chunk arrives: with one token partition from a window of about two macroblock rows of input (O(width) memory), with several from the VP8 chunk buffered whole. The ALPH plane is decoded whole with the header. Lossless, animated input and non-full options take the buffered fallback. Vp8Decoder.DecodeFrame is split into a resumable FrameState (ReconstructRow / FinishRow); the span path runs the same code and stays allocation-free. The bool decoder can resume from saved state and reports reading past its window, so a starved row is rolled back and decoded again once its bytes arrive. The last row waits until the container is checked to the RIFF end, so files the span decoder refuses fail here too. Tests: every fixture x 10 formats x 4 feeds equal TryDecodeInto; truncation, short partitions, late ALPH, 8192x8192 bounded memory, prefix reads, async end-to-end, streaming fuzz (WEBP_STREAM_FUZZ_ITERATIONS, soaked at 300000). The Webp benchmark times a Streaming case beside Current. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-macroblock Padded test in ReconstructRow made the span decode of lossy-opaque-1080p 8% slower (quick A/B, Ryzen 7 5800X); the streaming reader now tests it after the row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a macroblock row's output fits the rows asked for, the sink writes it to the destination instead of the band, saving a copy per row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unit 9 of the streaming batch: forward-only streaming decode of WebP lossy stills, on the Abstractions foundation (84b58f2).
What changes
WebpImageDecoderimplementsIStreamingImageDecoder;Registration.CreateRowReader = CreateRowReader(concrete method group). NewWebpRowReader : StreamingRowReader.IsStreamingstays true.DecodeOptionswait for the whole stream and useTryDecodeInto(IsStreaming == false);Infois then exactlyTryReadInfo's.Vp8Decoder.DecodeFrameis split into a resumableFrameStatestruct (Create/ReconstructRow/FinishRow/Save- andRestoreContexts). The span path runs the same code and still allocates nothing (WebpAllocationTests).Vp8BoolDecodergains a resume constructor (state + the next unread byte),Remaining, andPadded(set in the cold tail refill when it feeds zeros past its data).TryDecodeInto, and therefore libwebp 1.6.0.WebpContainer's rules. A file the span decoder refuses (truncated, bad trailing chunk, duplicates) therefore throwsInvalidDataExceptionhere too.TryDecodeInto:Infolacks chunks stored after the image (EXIF orientation, ICC, a late VP8X alpha flag).InvalidDataException, because they change the image after its pixels: an ALPH chunk after the VP8 chunk, and an animated VP8X after it.PublicAPI.Unshipped.txtlistsWebpImageDecoder.CreateRowReader.ab/quicktime aStreamingcase (aMemoryStream, 16 rows per read, checked equal toDecodeTobefore timing) and showstreaming ÷ current.Tests (
WebpStreamingDecodeTests, 87 cases)Fixtures/webpplus ImageMagick-encoded and our-encoder inputs, × 10 PixelFormats (incl. premultiplied, Bgra, Rgb, Gray, 16-bit), × feeds (one byte, random chunks, async-only, whole), viaStreamingAssert.RowsEqualDecodeInto.IsStreamingis asserted true for lossy (incl. partitions 2/4/8, lossy+alpha, VP8X) and false for lossless and animated input.TryDecodeIntofalse ⇔ streamingInvalidDataException.TryReadInfofor chunks before the image.webp-oracle-golden.tsv) throughReadRowsAsyncfrom an async-onlyChunkedStreamand an asyncFileStreamequalsTryDecodeInto.TryDecodeInto.Runs:
REQUIRE_ORACLES=1(dwebp / anim_dump / webpinfo 1.6.0): 622 passed (2 skips are the golden-regeneration switches).DOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0: 622 passed each.WEBP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~WebpStreamingDecode"(methodStreaming_fuzz): passed, about 20 s.Benchmark (AMD Ryzen 7 5800X, Windows 11 26200, .NET 10.0.15, AVX2, baseline 533b03e)
The machine was shared with ~47 dotnet processes from the parallel batch, so single runs move by ±8%. The figures are three
quick --rounds 40runs plus one BDNabrun:Span decode does not regress. A first version checked starvation inside the macroblock loop and measured 8% slower; it now checks once per row (second commit). Streaming is about 1.05× the span decode.
Needs outside this unit
WEBP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~WebpStreamingDecode"after touching the VP8 bool decoder,Vp8Decoder.FrameStateorWebpRowReader.CreateRowReaderto the registration, bound by concrete method group.🤖 Generated with Claude Code